import classNames from 'classnames';
import { useEffect, useRef } from 'react';
import styles from './index.module.scss';

interface TextareaType
  extends React.DetailedHTMLProps<
    React.TextareaHTMLAttributes<HTMLTextAreaElement>,
    HTMLTextAreaElement
  > {
  value: string;
}
export default function Textarea(props: TextareaType) {
  const textRef = useRef<HTMLTextAreaElement>(null);
  useEffect(() => {
    // textRef.current?.blur()
    textRef.current?.setSelectionRange(-1, -1);
  }, []);
  const {
    maxLength = 100,
    autoFocus = true,
    className,
    value,
    ...restProps
  } = props;
  return (
    <div className={styles.root}>
      {/* 文本输入框 */}
      <textarea
        ref={textRef}
        className={classNames('textarea', className)}
        value={value}
        {...restProps}
        autoFocus={autoFocus}
        maxLength={maxLength}
      />
      {/* 当前字数/最大允许字数 */}
      <div className="count">
        {value.length}/{100}
      </div>
    </div>
  );
}
